<?php
namespace App\Http\Controllers;

//use Illuminate\Http\Request;
use Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Models\UsersReg; //会员数据库
use Redirect; // Redirect::to() 方法 == Session::get('msg')方式取值
use Session;

class UsersRegController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
        // 会员注册页面
        return View('users.usreg');
    }

    public function uslogin()
    {
        //用户登录处理
        $username = request::input('username');
        $password = request::input('password'); 
        // $uspa = UsersReg::where('user_name', $username)->get();
        $uspa = UsersReg::where('email', $username)->orWhere('mobile', $username)->first();
        if($uspa != null) // 如果数据为空（用户名字不对）
        {
            $password1 = $uspa->password;
            $salt = $uspa->salt;
            $uid = $uspa->uid;
            $name = $uspa->user_name;

            $password = $this->compile_password($password, $salt); // 加密后的密码
            if($password == $password1) {
                // Session::put('name', $username);   //////session保存用户名字变量为 name
                Session::put('name', $name);
                session::put('nameid', $uid);      //////session保存用户名的uid变量为 nameid

                $uspa->login_count = $uspa->login_count + 1; // 记录登录次数
                $uspa->save();
                
                return 'ok';
            }
            else {
            return 'error';
            }
        }
        else {
            return 'error';
        }
    }

    public function uslogout()
    {
        //用户退出登录
        Session::forget('name');
        Session::forget('nameid');
        //return Redirect::to('books');
        return Redirect::back();
    }

    public function store(Request $request)
    {
        //禁止注册的名字
        $name = request::input('us');
        $tmparray = explode($name,'波');
        if(count($tmparray)>1){
            '%@#$###@#$ipzibo...';
        }
        // 禁止end


        //注册网站会员 保存
        $UsersReg = new UsersReg;
        $UsersReg->user_name = request::input('us');
        $username = request::input('us');
        $UsersReg->email = request::input('email');
        $UsersReg->mobile = request::input('mobile');
        $password = request::input('password1');
        $salt     = $this->fetch_salt(4);
        $UsersReg->password = $this->compile_password($password, $salt);
        $UsersReg->salt = $salt;

        $UsersReg->sex = 3; //性别默认保密 值=3
        //$UsersReg->reg_ip = $_SERVER['REMOTE_ADDR'];
        $UsersReg->reg_ip = $request->ip(); //注册ip
        $UsersReg->reg_time = time();
        //1超级管理员；2前台管理员；3未验证会员；4普通会员；99游客
        $UsersReg->group_id = 3; //3未验证会员
        $UsersReg->valid_email = 0;  //0邮件未认证；1已通过邮件认证
        $UsersReg->integral = 2000;  //新用户注册默认拥有积分(金币)
        $UsersReg->invitation_available = 5; // 可以使用的邀请会员数量
        //5注册会员；6初级会员；7中级会员；8高级会员；9核心会员
        $UsersReg->reputation_group = 5; // 名誉会员组 
        $UsersReg->reg_laiyuan = 'P'; // 标识会员通过电脑pc注册
        $UsersReg->save();  

        // 注册完成跳转到提示页面  
        return Redirect::to('regok')->with('username', $username);
    }

     /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  Request  $request
     * @param  int  $id
     * @return Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy($id)
    {
        //
    }

    public function usercheck()
    {
        //验证要注册的用户名字是否重复
        $username = $this->unescape(request::input('us'));
        //$UsersReg = new UsersReg;
        if(UsersReg::where('user_name', $username)->exists()){
            return '1';
        }
        else {
            return '0';
        }        
    }

    public function emailcheck()
    {
        //验证要注册的信箱是否重复
        $email = request::input('email');
        //$UsersReg = new UsersReg;
        if(UsersReg::where('email', $email)->exists()){
           return '1';
        }
        else{
            return '0';
        }
    }

    public function mobilecheck()
    {
        //验证要注册的手机号码是否重复
        $mobile = request::input('mobile');
        //$UsersReg = new UsersReg;
        if(UsersReg::where('mobile', $mobile)->exists()){
           return '1';
        }
        else{
            return '0';
        }
    }

 /**
 * 生成密码种子
 *
 * @param  integer
 * @return string
 */
public function fetch_salt($length = 4)
{
    for ($i = 0; $i < $length; $i++)
    {
        //$salt .= chr(rand(97, 122)); //不知道为什么 .= 运算符报错变量salt未定义
        $salt[$i] = chr(rand(97, 122));
    }

    $salt = $salt[0] . $salt[1] . $salt[2] . $salt[3]; //
    return $salt;
}

/**
 * 根据 salt 混淆密码
 *
 * @param  string
 * @param  string
 * @return string
 */
public function compile_password($password, $salt)
{
    $password = md5(md5($password) . $salt);

    return $password;
}

function unescape($str) {
//转换ajax传进来的中文字符为乱码的问题
    $str = rawurldecode($str);
    preg_match_all("/(?:%u.{4})|&#x.{4};|&#\d+;|.+/U",$str,$r);
    $ar = $r[0];
    //print_r($ar);
    foreach($ar as $k=>$v) {
        if(substr($v,0,2) == "%u"){
            $ar[$k] = iconv("UCS-2BE","UTF-8",pack("H4",substr($v,-4)));
  }
        elseif(substr($v,0,3) == "&#x"){
            $ar[$k] = iconv("UCS-2BE","UTF-8",pack("H4",substr($v,3,-1)));
  }
        elseif(substr($v,0,2) == "&#") {
            $ar[$k] = iconv("UCS-2BE","UTF-8",pack("n",substr($v,2,-1)));
        }
    }
    return join("",$ar);
}

}
 